#!/bin/sh

# run from directory where this script is
cd `dirname $0`
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use bands.x to check the band symmetry"
$ECHO "of fcc-Pt with a fully relativistic pseudo-potential including "
$ECHO "spin-orbit coupling. Also computes the projected DOS."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x bands.x"
PSEUDO_LIST="Pt.rel-pz-n-rrkjus.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
BANDS_COMMAND="$PARA_PREFIX $BIN_DIR/bands.x $PARA_POSTFIX"
PDOS_COMMAND="$PARA_PREFIX $BIN_DIR/projwfc.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO "  running bands.x as: $BANDS_COMMAND"
$ECHO "  running projwfc.x as: $PDOS_COMMAND"
$ECHO

# self-consistent calculation
cat > pt.scf.in << EOF
 &control
    calculation = 'scf'
    restart_mode='from_scratch',
    prefix='Pt',
    tprnfor = .true.,
    tstress =.true.,
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=  2, celldm(1) =7.42, nat=  1, ntyp= 1,
    lspinorb=.true.,
    noncolin=.true.,
    starting_magnetization=0.0,
    occupations='smearing',
    degauss=0.02,
    smearing='mv',
    ecutwfc =30.0,
    ecutrho =250.0,
 /
 &electrons
    mixing_beta = 0.7,
    conv_thr =  1.0d-8
 /
ATOMIC_SPECIES
Pt  0.0   Pt.rel-pz-n-rrkjus.UPF
ATOMIC_POSITIONS alat
Pt  0.0000000   0.00000000   0.0
K_POINTS AUTOMATIC
4 4 4 1 1 1
EOF
$ECHO "  running the scf calculation for Pt with spin-orbit coupling...\c"
$PW_COMMAND < pt.scf.in > pt.scf.out
check_failure $?
$ECHO " done"

# a non self-consistent calculation
cat > pt.nscf.in << EOF
 &control
    calculation = 'nscf'
    restart_mode='from_scratch',
    prefix='Pt',
    tprnfor = .true.
    pseudo_dir = '$PSEUDO_DIR/',
    outdir='$TMP_DIR/'
 /
 &system
    ibrav=  2, celldm(1) =7.42, nat=  1, ntyp= 1,
    lspinorb=.true.,
    noncolin=.true.,
    starting_magnetization=0.0,
    occupations='smearing',
    degauss=0.02,
    smearing='mv',
    ecutwfc =30.0,
    ecutrho =250.0,
 /
 &electrons
    mixing_beta = 0.7,
    conv_thr =  1.0d-8
 /
ATOMIC_SPECIES
Pt  0.0   Pt.rel-pz-n-rrkjus.UPF
ATOMIC_POSITIONS alat
Pt  0.0000000   0.00000000   0.0
K_POINTS
8
0.0 0.0 0.0 1.0
0.1 0.0 0.0 1.0
1.0 0.0 0.0 1.0
0.4 0.2 0.1 1.0
0.4 0.4 0.0 1.0
0.4 0.4 0.4 1.0
0.5 0.5 0.5 1.0
0.75 0.75 0.0 1.0
EOF
$ECHO "  running the non-scf calculation for Pt with spin-orbit coupling...\c"
$PW_COMMAND < pt.nscf.in > pt.nscf.out
check_failure $?
$ECHO " done"

cat > pt.bands.in << EOF
 &bands
    prefix='Pt',
    outdir='$TMP_DIR/'
    filband='pt.band'
    lsym=.true.,
 /
EOF

$ECHO "  running the symmetry analysis for Pt bands...\c"
$BANDS_COMMAND < pt.bands.in > pt.bands.out
check_failure $?
$ECHO " done"

cat > pt.pdos.in << EOF
 &projwfc
    prefix='Pt',
    outdir='$TMP_DIR/'
    filpdos='pt',
    emin=10, emax=25
 /
EOF

$ECHO "  running projected DOS for Pt with spin-orbit ...\c"
$PDOS_COMMAND -in pt.pdos.in > pt.pdos.out
check_failure $?
$ECHO " done"

$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/Pt.*

$ECHO
$ECHO "$EXAMPLE_DIR: done"
